package com.tgy.admin.Service.impl;

import com.tgy.admin.Service.AdminFileService;
import com.tgy.admin.util.BeanFieldUtils;
import com.tgy.core.service.BlogFileService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.BlogFile;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import com.tgy.util.AppFileUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:04
 *
 * 后台用户业务实现
 */
@Service
public class AdminFileServiceImpl implements AdminFileService {

    @Resource
    private BlogFileService blogFileService;
    @Resource
    private BeanFieldUtils beanFieldUtils;


    @Override
    public LayuiTableResult getBlogFileList(BlogFile bo, PageQuery pageQuery) {
        return blogFileService.queryLayuiPageList(bo,pageQuery);
    }


    @Override
    public GraceJSONResult addBlogFile(BlogFile bo) {
        bo.setCreatedTime(LocalDateTime.now());
        bo.setUpdatedTime(LocalDateTime.now());
        return GraceJSONResult.ok(blogFileService.insertByBo(bo));
    }

    @Override
    public GraceJSONResult updateBlogFile(BlogFile bo) {
        try {
            beanFieldUtils.update(bo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return GraceJSONResult.ok(blogFileService.updateByBo(bo));
    }

    @Override
    public GraceJSONResult delBlogFile(String id) {
        BlogFile blogFile = blogFileService.queryById(id);
        if (Objects.isNull(blogFile)){
            return GraceJSONResult.error("资源不存在");
        }
        Boolean aBoolean = blogFileService.deleteById(id);
        String filePath = blogFile.getFilePath();
        if (aBoolean){
            AppFileUtils.removeFileByPath(filePath);
        }
        return GraceJSONResult.ok();
    }

    @Override
    public GraceJSONResult delBlogFileIds(List<String> ids) {
        return GraceJSONResult.ok(blogFileService.deleteWithValidByIds(ids,false));
    }

    @Override
    public GraceJSONResult getBlogFileById(String id) {
        return GraceJSONResult.ok(blogFileService.queryById(id));
    }
}
